<template>
  <div class="EmployeeList">
    <h2>员工列表</h2>
    <div class="search">
      <el-input v-model="info" placeholder="输入名字进行搜索" clearable @input="info === '' ? flg = true : ''" @clear="flg = true"></el-input>
      <el-button type="primary" @click="queryData">查询</el-button>
      <el-button type="primary" @click="synchronization" :loading="loading">同步用户</el-button>
    </div>
      <el-table :data="flg ? tableData : searchTableData" border style="width: 100%" class="EmployeeList_table">
          <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>
          <el-table-column prop="name" label="姓名" align="center"></el-table-column>
          <el-table-column prop="department" label="部门" align="center"></el-table-column>
          <el-table-column prop="position" label="职位" align="center"></el-table-column>
          <el-table-column prop="phone" label="联系电话" align="center"></el-table-column>
          <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
          <el-table-column prop="permissions" label="权限范围" align="center">
            <template slot-scope="scope">
              <span v-for="(item, index) in scope.row.permissions" :key="index">{{item.name}}、</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <!-- 修改按钮 -->
              <el-button v-if="scope.row.name !== 'admin'" type="text" @click="showEditDialog(scope.row)">权限配置</el-button>
            </template>
          </el-table-column>
        </el-table>
      <el-dialog
        title="权限配置"
        :visible.sync="dialogVisible"
        width="30%">
        <el-tree
            :data="data"
            :expand-on-click-node="false"
            :highlight-current="false"
            :default-checked-keys="defKyes"
            show-checkbox
            default-expand-all
            node-key="id"
            :props="defaultProps"
            ref="tree"
            @node-click="handleNodeClick">
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="jurisdictionTo">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索框的数据
      info: '',
      // 权限管理的对话框
      dialogVisible: false,
      // 搜索数据显示的开关
      flg: true,
      // 表格的数据
      tableData: [],
      // 查询出来的数据
      searchTableData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 默认选中的节点id值数组
      defKyes: [],
      // 权限管理的权限选项
      data: [
        {
          id: 1,
          label: '后台管理员',
          children: [
            {
              id: 2,
              label: '贝伦管理'
            },
            {
              id: 3,
              label: '融汇管理'
            },
            {
              id: 4,
              label: '飞驰管理'
            },
            {
              id: 5,
              label: '系统管理'
            }
          ]
        },
        {
          id: 6,
          label: '大屏管理员',
          children: [
            {
              id: 7,
              label: '贝伦产业园区'
            },
            {
              id: 8,
              label: '融汇国际大厦'
            },
            {
              id: 9,
              label: '飞驰绿能'
            },
            {
              id: 10,
              label: '温泉冰雪体育公园项目'
            }
          ]
        }
      ],
      // data: [
      //   {
      //     id: 1,
      //     label: '后台管理员'
      //   },
      //   {
      //     id: 2,
      //     label: '贝伦管理'
      //   },
      //   {
      //     id: 3,
      //     label: '融汇管理'
      //   },
      //   {
      //     id: 4,
      //     label: '飞驰管理'
      //   },
      //   {
      //     id: 5,
      //     label: '系统管理'
      //   },
      //   {
      //     id: 6,
      //     label: '大屏管理员'
      //   },
      //   {
      //     id: 7,
      //     label: '贝伦产业园区'
      //   },
      //   {
      //     id: 8,
      //     label: '融汇国际大厦'
      //   },
      //   {
      //     id: 9,
      //     label: '飞驰绿能'
      //   },
      //   {
      //     id: 10,
      //     label: '温泉冰雪体育公园项目'
      //   }
      // ],
      // 当前点击员工的id
      userId: '',
      // 当前点击员工的姓名
      userName: '',
      // 同步用户
      loading: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    aaa (row) {
      console.log(row)
    },
    // 获取员工列表
    async getList () {
      const { data: res } = await this.$http.get('employee_list')
      this.tableData = res.data
    },
    // 查询按钮
    async queryData () {
      const { data: res } = await this.$http.post('search_employee', { name: this.info })
      this.searchTableData = res.data
      res.msg === 'success' ? this.flg = false : this.flg = true
    },
    // 打开权限管理
    showEditDialog (row) {
      this.dialogVisible = true
      this.$nextTick(function () {
        this.$refs.tree.setCheckedKeys([])
        this.userId = row.id
        this.userName = row.userName
        const arr = []
        row.permissions.forEach(item => arr.push(item.id))
        this.defKyes = arr
      })
    },
    // 确定分配
    async jurisdictionTo () {
      const { data: res } = await this.$http.post('change_permissions', {
        permission: this.$refs.tree.getCheckedKeys(),
        employee_id: this.userId
      })
      this.getList()
      this.dialogVisible = false
      if (res.msg === 'success') this.$message({ message: '分配成功', type: 'success' })
      const { data: res2 } = await this.$http.get('get_token')
      if (res2.data.name === this.userName) this.$store.commit('setPowerArr', res2.data.permissions)
    },
    // 实现点击文本选择该项，不一定需要点击正方形小选框；
    handleNodeClick (data) {
      // const arr = this.defKyes.filter(item => item !== data.id)
      // if (data) this.$refs.tree.setCheckedKeys(arr)
    },
    // 同步用户的按钮
    async synchronization () {
      this.loading = true
      const res = await this.$http.get('refresh_users')
      if (res.data === 'done') {
        this.loading = false
        this.getList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.EmployeeList{
  padding: 1rem;
  h2{
    font-size: 1.25rem;
    font-weight: 500;
  }
  .search{
    width: 50%;
    display: flex;
    align-items: center;
    padding: 1rem 0;
    .el-input{
      width: 45%;
    }
    .el-button{
      margin-left: 2rem;
      background-color: #275fdb;
      border-color: #275fdb;
      border-radius: 7px;
      padding: 10px 30px;
    }
  }
}
</style>
